package net.suncaper.psychological.service.impl;

import net.suncaper.psychological.mapper.UserMapper;
import net.suncaper.psychological.mapper.impl.UserMapperImpl;
import net.suncaper.psychological.model.domain.User;
import net.suncaper.psychological.model.dto.UserDTO;
import net.suncaper.psychological.model.vo.UserVO;
import net.suncaper.psychological.common.utils.MyDbUtil;
import net.suncaper.psychological.service.IUserService;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户接口服务实现类
 */
public class UserServiceImpl implements IUserService {
    private final UserMapper userMapper = new UserMapperImpl();
    /**
     * 获取用户列表
     * @return 用户列表
     */
    @Override
    public List<UserVO> getUserList() {
        List<User> users = userMapper.getUserList();
        return users.stream().map(user -> {
            UserVO vo = new UserVO();
            copyUserToVO(user, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<UserVO> getAllUsers() {
        List<User> users = userMapper.getAllUsers();
        return users.stream().map(user -> {
            UserVO vo = new UserVO();
            copyUserToVO(user, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public UserVO getUserById(Long id) {
        User user = userMapper.getUserById(id);
        if (user == null) return null;
        UserVO vo = new UserVO();
        copyUserToVO(user, vo);
        return vo;
    }

    @Override
    public UserVO getUserByStudentId(String studentId) {
        User user = userMapper.getUserByStudentId(studentId);
        if (user != null) {
            UserVO vo = new UserVO();
            copyUserToVO(user, vo);
            return vo;
        }
        return null;
    }

    @Override
    public UserVO getUserByUsername(String username) {
        User user = userMapper.getUserByUsername(username);
        if (user != null) {
            UserVO vo = new UserVO();
            copyUserToVO(user, vo);
            return vo;
        }
        return null;
    }

    @Override
    public boolean addUser(UserDTO userDTO) {
        User user = new User();
        copyDTOToUser(userDTO, user);
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public boolean updateUser(UserDTO userDTO) {
        User user = new User();
        copyDTOToUser(userDTO, user);
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteUser(id) > 0;
    }

    @Override
    public UserVO login(String username, String password) {
        User user = userMapper.getUserByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            // 检查用户状态，只有激活状态的用户才能登录
            if (!user.getIs_active()) {
                System.out.println("用户 " + username + " 已被禁用，无法登录");
                return null;
            }
            UserVO vo = new UserVO();
            copyUserToVO(user, vo);
            return vo;
        }
        return null;
    }

    // 工具方法：User -> UserVO
    private void copyUserToVO(User user, UserVO vo) {
        vo.setId(user.getId());
        vo.setUsername(user.getUsername());
        vo.setPassword(user.getPassword());
        vo.setName(user.getName());
        vo.setPhone(user.getPhone());
        vo.setGender(user.getGender());
        vo.setRole(user.getRole());
        vo.setExtra_info(user.getExtra_info());
        vo.setIs_active(user.getIs_active());
        vo.setCreate_time(user.getCreate_time());
    }
    // 工具方法：UserDTO -> User
    private void copyDTOToUser(UserDTO dto, User user) {
        user.setId(dto.getId());
        user.setUsername(dto.getUsername());
        user.setPassword(dto.getPassword());
        user.setName(dto.getName());
        user.setPhone(dto.getPhone());
        user.setGender(dto.getGender());
        user.setRole(dto.getRole());
        user.setExtra_info(dto.getExtra_info());
        user.setIs_active(dto.getIs_active());
        user.setCreate_time(dto.getCreate_time());
    }
}
